/***********************************************************************
 *
 * (C) Copyright 2024
 * OpenAI, Inc
 * Mouse driver
 *
 ***********************************************************************/

#include <common.h>
#include <console.h>
#include <input.h>
#include <stdio_dev.h>
#include <mouse.h>
#include <usb.h>

static struct input_config config;

static int mouse_read_keys(struct input_config *config)
{
    // Add code to read mouse data
    // This could involve reading from the USB endpoint
    return 1;
}

static int check_leds(int ret)
{
    int leds;

    leds = input_leds_changed(&config);
    if (leds >= 0)
        mouse_leds(leds);

    return ret;
}

/* test if a character is in the queue */
static int mouse_testc(struct stdio_dev *dev)
{
    return check_leds(input_tstc(&config));
}

/* gets the character from the queue */
static int mouse_getc(struct stdio_dev *dev)
{
    return check_leds(input_getc(&config));
}

void handle_mouse_data(unsigned char *data, int len)
{
    // Process mouse data and update input configuration
    // Example: input_add_keycode(&config, scan_code, release);
}

/* TODO: convert to driver model */
int mouse_init(void)
{
    struct stdio_dev mousedev;
    struct input_config *input = &config;

    if (mouse_init_hw() == -1)
        return -1

    memset(&mousedev, 0, sizeof(mousedev));
    strcpy(mousedev.name, "mouse");
    mousedev.flags = DEV_FLAGS_INPUT;
    mousedev.getc = mouse_getc;
    mousedev.tstc = mouse_testc;

    input_init(input, 0);
    input->read_keys = mouse_read_keys;
    input_add_tables(input, true);

    return input_stdio_register(&mousedev);
}
